﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"

//设置光标的坐标
void SetPos(int x, int y)
{
	COORD pos = { x,y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}
void CreateMap()
{
	int i = 0;
	//上下左右
	SetPos(0, 0);
	for (i = 0; i < 114; i += 2)
	{
		wprintf(L"%c", WALL);
	}
	SetPos(0, 52);
	for (i = 0; i < 114; i += 2)
	{
		wprintf(L"%c", WALL);
	}
	for(i = 1; i < 52; i++)
	{
		SetPos(0, i);
		wprintf(L"%c", WALL);
	}
	for (i = 1; i < 52; i++)
	{
		SetPos(112, i);
		wprintf(L"%c", WALL);
	}
	//打印提⽰信息
	SetPos(128, 30);
	printf("不能穿墙，不能咬自己\n");
	SetPos(128, 32);
	printf("利用↑.↓.←.→分别控制蛇的移动.");
	SetPos(128, 34);
	printf("F1 为加速，F2 为减速\n");
	SetPos(128, 36);
	printf("ESC ：退出游戏.space：暂停游戏.");
	SetPos(128, 40);
	printf("@你小子别偷懒了");
	
}
void WelcomToGame()
{
	SetPos(80, 30);
	printf("欢迎来到贪吃蛇大作战");
	SetPos(80, 50);
	system("pause");
	system("cls");
	SetPos(50, 25);
	printf("⽤ ↑.↓.←.→ 分别控制蛇的移动， F1为加速，F2为减速\n");
	SetPos(50, 26);
	printf("加速是给勇士的奖励.");
	SetPos(40, 25);
	system("pause");
	system("cls");
}
void InitSnake(pSnake ps)
{
	//创建一个单独的节点，这是为了创建蛇的身体
	pSnakeNode cur = NULL;
	int i = 0;
	//创建蛇身节点，并初始化坐标,利用头插法
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("malloc fail");
			return;
		}
		//如果cur不为空，就设置坐标
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;

		//然后给本游戏的蛇进行设置
		if (ps->pSnake == NULL)
		{
			ps->pSnake = cur;
		}
		else
		{
			cur->next = ps->pSnake;
			ps->pSnake = cur;
		}
	}
	//打印蛇
	cur = ps->pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps->SleepTime = 200;
	ps->score = 0;
	ps->status = OK;
	ps->dir = RIGHT;
	ps->FoodWeight = 10;
	ps->pFood = NULL;
}
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;

again:
	//x坐标为2的倍数,如果他一直不为2的倍数就一直算
	do
	{
		x = rand() % 109 + 2;
		y = rand() % 52 + 1;
	} while (x % 2 != 0);

	pSnakeNode cur = ps->pSnake;//获取指向蛇头的指针
	//食物不能和蛇身冲突
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}

	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));//创建食物
	if (pFood == NULL)
	{
		perror("malloc fail");
		return;
	}

		pFood->x = x;
		pFood->y = y;

		ps->pFood = pFood;
		SetPos(x, y);
		wprintf(L"%lc", FOOD);

	
	
}
void GameStart(pSnake ps)
{
	//设置控制台窗口的大小，60行，200列
	system("mode con cols=200 lines=60");
	//设置窗口名称
	system("title 贪吃蛇大作战");
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//隐藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态

	//打印欢迎界面
	WelcomToGame();
	//打印地图
	CreateMap();
	//初始化蛇
	InitSnake(ps);
	//创造第一个食物
	CreateFood(ps);
}
void pause()
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

int NextIsFood(pSnake ps, pSnakeNode pNext)
{
	if (ps->pFood->x == pNext->x && ps->pFood->y == pNext->y)
		return 1;//下一个坐标处是食物
	else
		return 0;
}

void EatFood(pSnake ps, pSnakeNode pNext)
{
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;

	//打印蛇
	pSnakeNode cur = ps->pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	ps->score += ps->FoodWeight;

	//释放旧的食物
	free(ps->pFood);
	//新建食物
	CreateFood(ps);
}

void NotEatFood(pSnake ps, pSnakeNode pNext)
{
	//头插法
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;

	//释放尾结点
	pSnakeNode cur = ps->pSnake;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//将尾节点的位置打印成空白字符
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	free(cur->next);
	cur->next = NULL;//易错
}


//检测是否撞墙
void KillByWall(pSnake ps)
{
	if (ps->pSnake->x == 0 ||
		ps->pSnake->x == 112 ||
		ps->pSnake->y == 0 ||
		ps->pSnake->y == 52)
	{
		ps->status = KILL_BY_WALL;
	}
}

//检测是否撞自己
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->pSnake->next;//从第二个节点开始
	while (cur)
	{
		if (cur->x == ps->pSnake->x && cur->y == ps->pSnake->y)
		{
			ps->status = KILL_BY_SELF;
			return;
		}
		cur = cur->next;
	}
}

void SnakeMove(pSnake ps)
{
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	pNext->next = NULL;

	switch (ps->dir)
	{
	case UP:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y - 1;
		break;
	case DOWN:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y + 1;
		break;
	case LEFT:
		pNext->x = ps->pSnake->x - 2;
		pNext->y = ps->pSnake->y;
		break;
	case RIGHT:
		pNext->x = ps->pSnake->x + 2;
		pNext->y = ps->pSnake->y;
		break;
	}

	//下一个坐标处是否是食物
	if (NextIsFood(ps, pNext))
	{
		//是食物就吃掉
		EatFood(ps, pNext);
	}
	else
	{
		//不是食物就正常一步
		NotEatFood(ps, pNext);
	}

	//检测撞墙
	KillByWall(ps);

	//检测撞到自己
	KillBySelf(ps);
}


void GameRun(pSnake ps)
{
	

	do
	{
		//当前的分数情况
		SetPos(124, 20);
		printf("总分:%5d\n", ps->score);
		SetPos(124, 22);
		printf("食物的分值:%02d\n", ps->FoodWeight);
		//检测按键
		//上、下、左、右、ESC、空格、F1、F2
		if (KEY_PRESS(VK_UP) && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = ESC;
			break;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//游戏要暂定
			pause();//暂定和回复暂定
		}
		else if (KEY_PRESS(VK_F1))
		{
			if (ps->SleepTime >= 80)
			{
				ps->SleepTime -= 30;
				ps->FoodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F2))
		{
			if (ps->FoodWeight > 2)
			{
				ps->SleepTime += 30;
				ps->FoodWeight -= 2;
			}
		}
		//走一步
		SnakeMove(ps);

		//睡眠一下
		Sleep(ps->SleepTime);

	} while (ps->status == OK);
}



void GameEnd(pSnake ps)
{
	SetPos(30, 24);
	switch (ps->status)
	{
	case ESC:
		printf("主动退出游戏，正常退出\n");
		break;
	case KILL_BY_WALL:
		printf("很遗憾，撞墙了，游戏结束\n");
		break;
	case KILL_BY_SELF:
		printf("很遗憾，咬到自己了，游戏结束\n");
		break;
	}

	pSnakeNode cur = ps->pSnake;

	//释放蛇身的节点
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}








